package auth

import (
	"context"
	"github.com/zeromicro/go-zero/core/collection"
	"go.uber.org/zap"
	"navi-mumbai/app/api/admin/internal/model/pg"
	"navi-mumbai/common/key/dbkey"
	"strconv"
	"strings"
	"time"
)

var adminPermission *ApiPermission
var strAdminPermissionMap string

func InitAdminPermission(mg pg.ManagerModel) *ApiPermission {
	adminPermission = newApiPermission(0)
	adminPermission.ManagerModel = mg
	adminPermission.writeApiPermissionLevelMap(SuperAdminAddManagerApi, AdminLevelAdminTop)
	adminPermission.writeApiPermissionLevelMap(SuperAdminQueryManagerListApi, AdminLevelAdminTop)
	adminPermission.writeApiPermissionLevelMap(SuperAdminUpdateManagerApi, AdminLevelAdminTop)
	adminPermission.writeApiPermissionLevelMap(SuperAdminQueryManagerLogListApi, AdminLevelAdminTop)
	adminPermission.writeApiPermissionLevelMap(SuperAdminQueryManagerLoginLogListApi, AdminLevelAdminTop)

	adminPermission.writeApiPermissionLevelMap(AdminQueryAppConfigListApi, AdminLevelAdminLow)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateAppConfigApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateBannerApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminQueryBannerListApi, AdminLevelAdminLow)

	adminPermission.writeApiPermissionLevelMap(AdminBanUserApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminUnbanUserApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminQueryUserListApi, AdminLevelAdminLow)
	adminPermission.writeApiPermissionLevelMap(AdminQueryUserFeedbackListApi, AdminLevelAdminLow)
	adminPermission.writeApiPermissionLevelMap(AdminReplyUserFeedbackApi, AdminLevelAdminMiddle)

	adminPermission.writeApiPermissionLevelMap(AdminQueryUserDepositListApi, AdminLevelAdminLow)
	adminPermission.writeApiPermissionLevelMap(AdminQueryUserWithdrawListApi, AdminLevelAdminLow)

	adminPermission.writeApiPermissionLevelMap(AdminAuditUserWithdrawApi, AdminLevelAdminMiddle)
	//adminPermission.writeApiPermissionLevelMap(AdminAddWalletExchangeConfigApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateWalletExchangeConfigApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminQueryWalletExchangeConfigListApi, AdminLevelAdminLow)

	adminPermission.writeApiPermissionLevelMap(AdminQueryGameOrderListApi, AdminLevelAdminLow)
	//adminPermission.writeApiPermissionLevelMap(AdminAddWalletMoneyConfigApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateWalletMoneyConfigApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminQueryWalletMoneyConfigListApi, AdminLevelAdminLow)

	//adminPermission.writeApiPermissionLevelMap(AdminAddWalletPayChannel, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateWalletPayChannel, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminQueryWalletPayChannelList, AdminLevelAdminLow)

	adminPermission.writeApiPermissionLevelMap(AdminPushUserWithdrawApi, AdminLevelAdminTop)

	//adminPermission.writeApiPermissionLevelMap(AdminAddGameApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateGameApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminQueryGameListApi, AdminLevelAdminLow)

	//adminPermission.writeApiPermissionLevelMap(AdminAddGameProviderApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateGameProviderApi, AdminLevelAdminMiddle)
	adminPermission.writeApiPermissionLevelMap(AdminQueryGameProviderListApi, AdminLevelAdminLow)
	adminPermission.writeApiPermissionLevelMap(AdminQueryGameMoneyExchangeConfigListApi, AdminLevelAdminLow)

	adminPermission.writeApiPermissionLevelMap(AdminQueryUserDepositStatApi, AdminLevelAdminAdvanced)
	adminPermission.writeApiPermissionLevelMap(AdminQueryUserWithdrawStatApi, AdminLevelAdminAdvanced)
	adminPermission.writeApiPermissionLevelMap(AdminQueryUserGameStatApi, AdminLevelAdminAdvanced)

	adminPermission.writeApiPermissionLevelMap(AdminQueryLtvApi, AdminLevelAdminAdvanced)
	adminPermission.writeApiPermissionLevelMap(AdminQueryDataBoardApi, AdminLevelAdminAdvanced)

	adminPermission.writeApiPermissionLevelMap(AdminServiceUpdateConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateServiceUpdateConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminDeleteServiceUpdateConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminQueryServiceUpdateConfigApi, AdminLevelSuperAdmin)

	adminPermission.writeApiPermissionLevelMap(AdminMarqueeConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateMarqueeConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminDeleteMarqueeConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminQueryMarqueeConfigApi, AdminLevelSuperAdmin)

	adminPermission.writeApiPermissionLevelMap(AdminUserInviteConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateUserInviteConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminQueryUserInviteConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminQueryUserInviteOrderFlowApi, AdminLevelSuperAdmin)

	adminPermission.writeApiPermissionLevelMap(AdminQueryUserInviteApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminChangeBindingApi, AdminLevelSuperAdmin)

	adminPermission.writeApiPermissionLevelMap(AdminDeleteActivityConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateActivityConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminCreateActivityConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminQueryActivityConfigApi, AdminLevelSuperAdmin)

	adminPermission.writeApiPermissionLevelMap(AdminApkConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminUpdateApkConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminDeleteApkConfigApi, AdminLevelSuperAdmin)
	adminPermission.writeApiPermissionLevelMap(AdminQueryApkConfigApi, AdminLevelSuperAdmin)

	strAdminPermissionMap = adminPermission.getApiPermissionLevelMapJsonStr()
	var err error
	adminPermission.adminApiPermissionCache, err = collection.NewCache(7*24*time.Hour, collection.WithLimit(100))
	if err != nil {
		logger.Errorf("err == ", zap.Error(err))
		panic(err)
	}
	return adminPermission
}

// verify permissions level
func (apm *ApiPermission) VerifyPermissionLevel(ctx context.Context, api int64, uid int64, level int64) bool {
	// 校验权限范围
	lev := apm.readApiPermissionLevelMap(api)
	if lev <= 0 || lev > level {
		return false
	}

	if level >= AdminLevelSuperAdmin {
		return true
	}

	// 校验api权限
	if !apm.existAdminApiPermission(ctx, uid, api) {
		return false
	}
	return true
}

func (apm *ApiPermission) GetPermissionDefine() string {
	return strAdminPermissionMap
}

func (apm *ApiPermission) SetAdminApiPermission(uid int64, allowApi string) {
	if allowApi == "" {
		return
	}

	m := parseAllowApiMap(allowApi)
	apm.setAdminApiPermission(uid, m)
}

func parseAllowApiMap(str string) map[int64]bool {
	m := make(map[int64]bool, 0)
	sa := strings.Split(str, dbkey.Separator)
	if len(sa) <= 0 {
		return m
	}
	for _, v := range sa {
		if v == "" {
			continue
		}
		api, err := strconv.ParseInt(v, 10, 64)
		if err != nil {
			logger.Errorf("err == ", zap.Error(err))
			continue
		}
		m[api] = true
	}
	return m
}
